package com.jnu.myfirstapplication.recycleItem;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.jnu.myfirstapplication.R;
import com.jnu.myfirstapplication.data.Book;

import java.util.Collections;
import java.util.List;

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.DataViewHolder> implements View.OnClickListener,ItemTouchCallBack.OnItemTouchListener {
    private Context context;
    private List<Book> list;

    public GridAdapter(Context context, List<Book> list) {
        this.context = context;
        this.list = list;
    }

    /**
     * 创建ViewHolder
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载item布局文件(每一个)
        View view = LayoutInflater.from(context).inflate(R.layout.item_main,null);

        //为View设置单击事件
        view.setOnClickListener(this);
        DataViewHolder viewHolder = new DataViewHolder(view);

        //使用代码设置宽高（xml布局设置无效时）
        view.setLayoutParams(new RecyclerView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        return viewHolder;
    }

    /**
     * 绑定数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(DataViewHolder holder, int position) {
        //设置每一个Item的高度
        holder.textView.setText(list.get(position).getTitle());
    }

    /**
     * 选项总数
     * @return
     */
    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * 单击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        if(onItemClickListener!=null){
            int position = recyclerView.getChildAdapterPosition(v);
            //程序执行到此，会执行该方法的具体实现
            onItemClickListener.onItemClick(recyclerView,v,position,list.get(position));
        }
    }

    @Override
    public void onMove(int fromPosition, int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(list, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(list, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onSwiped(int position) {
        Log.i("drag","onSwiped");
        list.remove(position);
        notifyItemRemoved(position);
    }

    /**
     *  RecycleView中针对ViewHolder来实现
     */
    public static class DataViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public DataViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.textView_item_caption);
        }
    }
    private RecyclerView recyclerView;
    private  OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    /**
     * 设计recycleView选项单击事件的回调接口（给外面使用）
     * 参考ListView选项单击事件方法
     */
    public interface OnItemClickListener{
        //参数（父组件，点击的View，位置，这里可能是某个对象的id或对象/这里不需要）
        void onItemClick(RecyclerView recyclerView, View view, int position, Book obj);
    }
    /**
     *   将RecycleView附加到Adapter上
     */
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView= recyclerView;
    }
    /**
     *   将RecycleView从Adapter解除
     */
    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }
}
